package com.yiwu.common.utils;

import com.yiwu.common.enums.ResponseCode;
import com.yiwu.common.exception.BaseResponse;
import com.yiwu.common.exception.BusinessException;

/**
 * @author 咏鹅
 * @version 1.0
 * @description 统一结果返回工具
 * @date 2024/1/3 9:48
 */
public class ResultUtil {

    // 成功请求
    public static <T> BaseResponse<T> success(T data) {
        return new BaseResponse<T>(200, "OK", data);
    }

    // 失败请求
    public static <T> BaseResponse<T> fail(ResponseCode responseCode, String msg) {
        return new BaseResponse<T>(responseCode.getCode(), msg);
    }

    public static <T> BaseResponse<T> fail(int errorCode,String msg) {
        return new BaseResponse<T>(errorCode, msg);
    }

    public static <T> BaseResponse<T> fail(ResponseCode responseCode) {
        return new BaseResponse<T>(responseCode.getCode(), responseCode.getMessage());
    }

    public static <T> BaseResponse<T> fail(BusinessException bus) {
        return new BaseResponse<T>(bus.getCode(), bus.getMessage());
    }

    public static <T> BaseResponse<T> fail (int errorCode , String msg , T data) {
        return new BaseResponse<T>(errorCode,msg,data);
    }
}
